//
//  YuYueHeaderView.m
//  FD
//
//  Created by xiaofang wang on 2018/11/8.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "YuYueHeaderView.h"
//#import "YuYueLabelTextField.h"

//h:220

@interface YuYueHeaderView()

/**3.选择性别sexView*/
@property (nonatomic, strong) UIView *sexView;
/**先生按钮 默认选中❌    2019-2-14默认不选中*/
@property (nonatomic, strong) UIButton *manBtn;
/**女士按钮*/
@property (nonatomic, strong) UIButton *womanBtn;


@end

@implementation YuYueHeaderView
- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        [self settupView];
        
        //
//        self.manBtn.selected  = YES;
//        self.sex = @(0); //默认选中的是先生
    }
    return self;
}

- (void)settupView {
    [self addSubview:self.numberTF];
    [self addSubview:self.lianxirenTF];
    
    [self addSubview:self.sexView];
    [self.sexView addSubview:self.manBtn];
    [self.sexView addSubview:self.womanBtn];
    
    [self addSubview:self.phoneTf];    
}

//国际化
//- (void)__international {
//    NSString *numStr = NSLocalizedString(Home_have_dinner_number, @"用餐人数");
//
//    self.numberTF.placeholder =
//}

#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];

    [self.numberTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.mas_top).offset(10);
        make.height.mas_equalTo(50);
        
    }];
    [self.lianxirenTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.numberTF.mas_bottom).offset(1);//线
        make.height.mas_equalTo(50);
    }];
    
    //===============
    [self.sexView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.lianxirenTF.mas_bottom).offset(1);
        make.height.mas_equalTo(50);
    }];
    [self.manBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.sexView.mas_left).offset(60);
        make.centerY.equalTo(self.sexView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(100, 20));
    }];
    [self.womanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.manBtn.mas_right);
        make.centerY.equalTo(self.sexView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(100, 20));
    }];
    //===============
    
    [self.phoneTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.sexView.mas_bottom).offset(1);
        make.height.mas_equalTo(50);
    }];
}


#pragma mark - getter
- (YuYueLabelTextField *)numberTF {
    if (!_numberTF) {
        //国际化词条
        
        NSString *numStr = NSLocalizedString(Home_have_dinner_number, @"用餐人数");
        NSString *pleaseNumberStr = NSLocalizedString(Home_put_your_have_dinner_number, @"请选择用餐人数");
        _numberTF = [[YuYueLabelTextField alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50) leftTitle:numStr placeholder:pleaseNumberStr];
    }
    return _numberTF;
}

- (YuYueLabelTextField *)lianxirenTF {
    if (!_lianxirenTF) {
        //国际化词条
        NSString *lianxirenStr = NSLocalizedString(Home_contacts, @"联系人");
        NSString *pleaseLianXiRenStr = NSLocalizedString(Home_put_your_contacts_name, @"请选择用餐人数");

        _lianxirenTF = [[YuYueLabelTextField alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50) leftTitle:lianxirenStr placeholder:pleaseLianXiRenStr];
    }
    return _lianxirenTF;
}
- (UIView *)sexView {
    if (!_sexView) {
        _sexView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
        _sexView.backgroundColor = [UIColor whiteColor];
    }
    return _sexView;
}
- (UIButton *)manBtn {
    if (!_manBtn) {
        //国际化词条
        NSString *manStr = NSLocalizedString(Home_Man, @"先生");

        _manBtn = [self createBtnWithTitle:manStr target:self action:@selector(clickManAction:)];
    }
    return _manBtn;
}

- (UIButton *)womanBtn {
    if (!_womanBtn) {
        NSString *womanStr = NSLocalizedString(Home_Woman, @"女士");
        _womanBtn = [self createBtnWithTitle:womanStr target:self action:@selector(clickWomanAction:)];
    }
    return _womanBtn;
}

- (YuYueLabelTextField *)phoneTf {
    if (!_phoneTf) {
        //国际化词条
        NSString *phoneStr = NSLocalizedString(Home_phone, @"电话");
        NSString *pleasePhoneStr = NSLocalizedString(Home_inputPhone, @"请输入联系电话");

        _phoneTf = [[YuYueLabelTextField alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50) leftTitle:phoneStr placeholder:pleasePhoneStr];
    }
    return _phoneTf;
}


- (UIButton *)createBtnWithTitle:(NSString *)title target:(id)target action:(SEL)action {

    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [btn setTitle:title forState:(UIControlStateNormal)];
    [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [btn setImage:[UIImage imageNamed:@"unselected"] forState:(UIControlStateNormal)];
    [btn setImage:[UIImage imageNamed:@"selected"] forState:(UIControlStateSelected)];
    
    //添加点击事件
    [btn addTarget:target action:action forControlEvents:(UIControlEventTouchUpInside)];
    return btn;
}


#pragma mark - 获取textField

#pragma mark - 点击事件
- (void)clickManAction:(UIButton *)btn
{
    btn.selected = YES;
    self.womanBtn.selected = NO;
    
    self.sex = @(0);//先生
    FDLog(@"点击的是:%@",btn.titleLabel.text);
}

- (void)clickWomanAction:(UIButton *)btn
{
    btn.selected = YES;
    self.manBtn.selected = NO;
    
    self.sex = @(1);//女士
    FDLog(@"点击的是:%@",btn.titleLabel.text);
}

#pragma mark - 2019-2-19
/**2019-2*-19 显示上次选择的性别*/
- (void)updateSexWithSexNumber:(NSNumber *)sex {

    if ([sex isEqualToNumber:@(0)]) {
        self.manBtn.selected = YES;
        self.womanBtn.selected = NO;
    }else if ([sex isEqualToNumber:@(1)]) {
        self.manBtn.selected = NO;
        self.womanBtn.selected = YES;
    }
}

@end
